package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;

import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;

    /**
     * 商品的分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageDTO getPage(Integer pageNum, Integer pageSize) {
        IPage<Item> page = new Page<>(pageNum,pageSize);
        itemMapper.selectPage(page,null);
        List<Item> records = page.getRecords();
        long total = page.getTotal();
        return new PageDTO(total,records);
    }

    /**
     * 根据id删除数据
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        itemMapper.deleteById(id);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @Override
    public void updateItemStatus(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        //调用mapper层
        itemMapper.updateById(item);
    }

    /**
     * 修改商品
     * @param item
     */
    @Override
    public void updateItem(Item item) {
        itemMapper.update(item,null);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @Override
    public void updateStock(Long itemId, Integer num) {
        itemMapper.updateStock(itemId,num);
    }
}
